package com.youchuang.redistest.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.youchuang.redistest.bean.Team;
import com.youchuang.redistest.dao.TeamDAO;
import com.youchuang.redistest.service.TeamService;

@Service("teamService")
@Transactional
public class TeamServiceImpl extends CacheServiceImpl<Team, TeamDAO> implements TeamService {

	@Override
	public String getSearchKeySet() {
		return SEARCH_KEY_SET;
	}

	@Override
	protected String getKeyPrefix() {
		return CACHE_KEY_ENTITY;
	}

	@Override
	protected Team constructEntityFromCache(RedisConnection connection, RedisSerializer<String> serializer,
			byte[] entityKey) {
		List<byte[]> data = connection.hMGet(entityKey, serializer.serialize("id"), serializer.serialize("name"),
				serializer.serialize("userId"));
		if (data.get(0) == null) {
			return null;
		}
		String userId = data.get(2) == null ? null : serializer.deserialize(data.get(2));
		return new Team(serializer.deserialize(data.get(0)), serializer.deserialize(data.get(1)), userId);
	}

	@Override
	protected void cacheSearchResultIndex(RedisConnection connection, RedisSerializer<String> serializer,
			byte[] srchKey, List<Team> result) {
		for (Team team : result) {
			connection.rPush(srchKey, serializeEntityKey(serializer, team.getId()));
		}
	}

	@Override
	protected byte[] saveCache(Team bean, RedisConnection connection) {
		RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
		byte[] key = serializeEntityKey(serializer, bean.getId());
		BoundHashOperations<Serializable, byte[], byte[]> opts = redisTemplate.boundHashOps(key);
		opts.put(serializer.serialize("id"), serializer.serialize(bean.getId().toString()));
		opts.put(serializer.serialize("name"), serializer.serialize(bean.getName().toString()));
		if (bean.getUserId() != null && !"".equals(bean.getUserId())) {
			opts.put(serializer.serialize("userId"), serializer.serialize(bean.getUserId().toString()));
		} else {
			opts.put(serializer.serialize("userId"), null);
		}
		connection.hMSet(key, opts.entries());
		return key;
	}

	@Override
	protected void updateCache(Team bean, RedisConnection connection) {
		RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
		byte[] key = serializeEntityKey(serializer, bean.getId());
		RedisConnection conn = redisTemplate.getConnectionFactory().getConnection();
		Boolean hasKey = conn.exists(key);
		if (hasKey) {
			if (!StringUtils.isEmpty(bean.getName())) {
				connection.hSet(key, serializer.serialize("name"), serializer.serialize(bean.getName()));
			}
			if (bean.getUserId() != null && !"".equals(bean.getUserId())) {
				connection.hSet(key, serializer.serialize("userId"), serializer.serialize(bean.getUserId().toString()));
			} else {
				connection.hSet(key, serializer.serialize("userId"), null);
			}
		} else {
			saveCache(bean, connection);
			// TODO:这步会影响,key为getKeyPrefix()+CACHE_LIST的列表数据不同步
		}

	}

}
